﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Commands;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;
using System.Linq;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmFranchisorPayment : Form
    {
        public frmFranchisorPayment()
        {
            InitializeComponent();
        }

        private void frmFranchisorPayment_Load(object sender, EventArgs e)
        {

        }

        #region FranchisePayments
        private void btnFranchiserPayNew_Click(object sender, EventArgs e)
        {
            txtFranchiserPayId.Text = Franchise_PaymentDAO.GetNewFranchiserPayId().ToString();

        }


        private void FillFranchiserCurrentBalance()
        {

            List<Franchise_Payment> LvList = Franchise_PaymentDAO.GetFranchiseAvailableBalance(txtFranchiserPayFrnID.Text.ToString());


            foreach (Franchise_Payment LvFranchisePay in LvList)
            {

                txtFranchiserDepositBalance.Text = LvFranchisePay.Balance.ToString();
            }


        }

        private void txtFranchiserPayFrnID_Enter(object sender, EventArgs e)
        {
            FillFranchiserCurrentBalance();
        }

        private void txtFranchiserPayFrnID_MouseClick(object sender, MouseEventArgs e)
        {
            FillFranchiserCurrentBalance();
        }


        private int SaveFranchiserPayment()
        {

            Franchise_Payment LvFrn = CollectFranchiserPayFigures();
            if (LvFrn != null)
            {
                return Franchise_PaymentDAO.SaveFranchisePay(LvFrn);
            }
            else
            {
                return 0;
            }
        }

        private Franchise_Payment CollectFranchiserPayFigures()
        {

            try
            {

                Franchise_Payment LvFrn = new Franchise_Payment();

                LvFrn.FP_Id = txtFranchiserPayId.Text.ToString();
                LvFrn.F_Id = txtFranchiserPayFrnID.Text.ToString();
                LvFrn.Deposit = decimal.Parse(txtFranchiserPayDeposit.Text.ToString());
                LvFrn.Payment_Stat = "OK";
                LvFrn.User_Create = "1";
                LvFrn.Date_Create = DateTime.Now;
                LvFrn.Last_Modify = DateTime.Now;


                return LvFrn;
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void btnFranchiserPaySave_Click(object sender, EventArgs e)
        {
            SaveFranchiserPayment();
            MessageBox.Show("FRANCHISE PAYMENT STATUS WAS BSUCCESFULLY POSTED");
        }

        private void btnFranchiserPayClear_Click(object sender, EventArgs e)
        {
            txtFranchiserPayDeposit.Text = "";
            txtFranchiserPayFrnID.Text = "";
            txtFranchiserPayId.Text = "";
            txtFranchiserDepositBalance.Text = "";
        }

        private void btnFrnPaySearchbyDate_Click(object sender, EventArgs e)
        {
            FranchisePayFillGridByFrnID();
        }


        private void FranchisePayFillGridByFrnID()
        {
            //Fill the DataGrid
            List<Franchise_Payment> LvList = Franchise_PaymentDAO.GetFranchisePayByFrnId(txtFrnpaySearchFrId.Text.ToString(), dtpFrnPayFrom.Value, dtpFrnPayTo.Value);
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            dgvFranchisePay.DataSource = LvTable;
            LvTable.Columns.Add("FrnId ");
            LvTable.Columns.Add("FrnPayID ");
            LvTable.Columns.Add("Deposit ");
            LvTable.Columns.Add("Balance ");
            LvTable.Columns.Add("PaymentStatus ");
            LvTable.Columns.Add("DateCreated ");


            foreach (Franchise_Payment LvFrnPay in LvList)
            {
                LvTable = (DataTable)dgvFranchisePay.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvFrnPay.F_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvFrnPay.FP_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvFrnPay.Deposit.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvFrnPay.Balance.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][4] = LvFrnPay.Payment_Stat.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][5] = LvFrnPay.Date_Create.ToString();


            }

        }
        #endregion

        private void btnFranchiserPaySave_Click_1(object sender, EventArgs e)
        {
            SaveFranchiserPayment();
            MessageBox.Show("FRANCHISE PAYMENT STATUS WAS BSUCCESFULLY POSTED");
        }
    }
}
